package com.cyy.system.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.cyy.common.domain.Response;
import com.cyy.common.pojo.dto.RouterDto;
import com.cyy.common.pojo.dto.UserFormDto;
import com.cyy.common.pojo.query.system.SystemUserQuery;
import com.cyy.common.service.UserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户")
@RestController
@RequestMapping("/system/user")
@RequiredArgsConstructor
@SaCheckLogin
public class UserController {


    private final UserService userService;

    @GetMapping("/getUserInfo")
    public Response getUserInfo() {
        return Response.success().data(userService.getUserInfo());
    }

    @PostMapping("/getUserMenus")
    public Response getUserMenus() {
        List<RouterDto> userMenus = userService.getUserMenus();
        return Response.success().data(userMenus);
    }

    @GetMapping("/page")
    public Response getPage(SystemUserQuery query) {
        return Response.success().data(userService.getPage(query));
    }

    @GetMapping("/{userId}/form")
    public Response getUserForm(@PathVariable("userId") Integer userId) {
        return Response.success().data(userService.getUserForm(userId));
    }

    @PostMapping("")
    public Response addUser(@RequestBody UserFormDto req) {
        userService.addUser(req);
        return Response.success();
    }

    @PutMapping("")
    public Response updateUser(@RequestBody UserFormDto req) {
        userService.updateUser(req);
        return Response.success();
    }

    @PatchMapping("/{id}/password")
    public Response updateUserPassword(@PathVariable("id") Integer id,
                                       @RequestParam("password") @NotBlank String password) {
        userService.updateUserPassword(id, password);
        return Response.success();
    }

    @DeleteMapping("/{ids}")
    public Response deleteUsers(@PathVariable("ids") String ids) {
        userService.deleteUsers(ids);
        return Response.success();
    }
}
